import re
import csv

# class="tb_icon_author "
#           title="主题作者: (.*?)"
#
# class="threadlist_abs threadlist_abs_onlyline ">
#             (.*?)
#         <
#
# title="创建时间">2021-08</span>

with open('666.txt', 'r', encoding='UTF-8') as f:
    source = f.read()

result_list = []
user_name_list = re.findall('title="主题作者: (.*?)"', source, re.S)
content_list = re.findall('threadlist_abs threadlist_abs_onlyline ">(.*?)<', source, re.S)
create_time_list = re.findall('创建时间">(.*?)<', source, re.S)

for i in range(len(content_list)):
    result = {'username': user_name_list[i], 'content': content_list[i], 'create_time': create_time_list[i]}
    result_list.append(result)

print(len(user_name_list))
print(len(content_list))
print(len(create_time_list))

with open('tb.scv', 'w', encoding='UTF-8') as f:
    writer = csv.DictWriter(f, fieldnames=['username', 'content', 'create_time'])
    writer.writeheader()
    writer.writerows(result_list)


